package com.evolution.warlock.controller.admin;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.evolution.warlock.Message;
import com.evolution.warlock.Page;
import com.evolution.warlock.Pageable;
import com.evolution.warlock.entity.Admin;
import com.evolution.warlock.entity.Area;
import com.evolution.warlock.entity.CriminalBase;
import com.evolution.warlock.entity.CriminalMember;
import com.evolution.warlock.entity.MasterConfig.KeyType;
import com.evolution.warlock.service.AdminService;
import com.evolution.warlock.service.AreaService;
import com.evolution.warlock.service.CriminalBaseService;
import com.evolution.warlock.service.EthnicService;
import com.evolution.warlock.service.MasterConfigService;
import com.evolution.warlock.service.ReligionService;

@Controller("criminalController")
@RequestMapping("/admin/criminalbase")
public class CriminalController extends BaseController {

	@Resource(name = "adminServiceImpl")
	private AdminService adminService;
	@Resource(name = "ethnicServiceImpl")
	private EthnicService ethnicService;
	@Resource(name = "religionServiceImpl")
	private ReligionService religionService;
	@Resource(name = "masterConfigServiceImpl")
	private MasterConfigService masterConfigService;
	@Resource(name = "criminalBaseServiceImpl")
	private CriminalBaseService criminalBaseService;
	@Resource(name = "areaServiceImpl")
	private AreaService areaService;

	/**
	 * 增加
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap model) {
		Admin admin = adminService.getCurrent();
		model.addAttribute("departmentName", admin.getDepartment());
		model.addAttribute("policerName", admin.getName());
		model.addAttribute("commonHave", masterConfigService.findList(KeyType.COMMON_HAVE.getValue()));
		model.addAttribute("commonBool", masterConfigService.findList(KeyType.COMMON_BOOLEAN.getValue()));
		model.addAttribute("ethnicGroup", ethnicService.findListOrder("periodCd"));
		model.addAttribute("religionGroup", religionService.findListOrder("periodCd"));
		model.addAttribute("genderGroup", masterConfigService.findList(KeyType.GENDER.getValue()));
		model.addAttribute("marriageGroup", masterConfigService.findList(KeyType.MARRIAGE_STATUS.getValue()));
		model.addAttribute("educationGroup", masterConfigService.findList(KeyType.EDUCATION_LEVEL.getValue()));
		model.addAttribute("mainIncomeGroup", masterConfigService.findList(KeyType.TEMPORARY_INCOME.getValue()));
		model.addAttribute("professionGroup", masterConfigService.findList(KeyType.PROFESSION.getValue()));
		model.addAttribute("penaltyCategoryGroup", masterConfigService.findList(KeyType.PENALTY_CATEGORY.getValue()));
		model.addAttribute("criminalCategoryGroup", masterConfigService.findList(KeyType.CRIMINAL_CATEGORY.getValue()));
		model.addAttribute("temporaryNoRegistDuraGroup",
				masterConfigService.findList(KeyType.TEMPORARY_NO_REGIST_DURA.getValue()));
		model.addAttribute("temporaryTransToolsGroup",
				masterConfigService.findList(KeyType.TEMPORARY_TRANS_TOOL.getValue()));
		model.addAttribute("durationStayGroup", masterConfigService.findList(KeyType.DURATION_STATUS.getValue()));
		model.addAttribute("localAreaCategoryGroup",
				masterConfigService.findList(KeyType.CIVIL_DIS_FOOTHOLD_PLACE.getValue()));
		return "/admin/criminalbase/add";
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/save", method = RequestMethod.GET)
	public String save(HttpServletRequest request, HttpServletResponse response, Long criminalBaseId,
			RedirectAttributes redirectAttributes) {

		// CriminalBase criminalBase = criminalBaseService.find(criminalBaseId);

		for (Iterator iter = request.getParameterMap().entrySet().iterator(); iter.hasNext();) {
			Map.Entry element = (Map.Entry) iter.next();
			Object strKey = element.getKey();
			String[] values = (String[]) element.getValue();

			System.out.println("strKey: " + strKey.toString());
			for (String str : values) {
				System.out.println("Value :" + str);
			}
		}

		// Iterator keyIterator = request.getParameterMap().keySet().iterator();
		// Iterator valuesIterator =
		// request.getParameterMap().values().iterator();
		// while(keyIterator.hasNext()){
		// Object keyName = keyIterator.next();
		// System.out.println("keyName :"+keyName);
		// System.out.println("value :"+request.getParameterMap().get(keyName));
		// }
		//
		//
		//
		// while(valuesIterator.hasNext()){
		// String[] values = (String[])valuesIterator.next();
		// for(String str : values){
		// System.out.println("Str :"+str);
		// }
		// }

		// set the status
		// criminalBase.setStatus("0");
		// Calendar calender = Calendar.getInstance();
		// criminalBase.setCriminalNumber(Long.valueOf(calender.getTimeInMillis()));
		// Admin admin = adminService.getCurrent();
		// criminalBase.setPolicerDepartment(admin.getDepartment());
		// criminalBase.setPolicerName(admin.getName());
		// criminalBaseService.save(criminalBase);
		// addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		// if(null!=caseCriminal && !"".equals(caseCriminal)){
		// return
		// "redirect:/admin/robsteal/add.jhtml?criminalNumber="+criminalBase.getCriminalNumber();
		// }
		//
		// if(null!=casePenalty && !"".equals(casePenalty)){
		// return
		// "redirect:/admin/civildispute/add.jhtml?criminalNumber="+criminalBase.getCriminalNumber();
		// }

		return "redirect:/admin/civildispute/add.jhtml";
	}

	// @RequestMapping(value = "/list", method = RequestMethod.GET)
	// public String list(Pageable pageable, ModelMap model, String idCardNum ,
	// String name, String criminalCategory, String penaltyCategory, String
	// temporaryGuy, String currentArea, Date fromDate, Date toDate) {
	// //convert name String
	// if(null != name){
	// System.out.println("Before name :"+name);
	// }
	// System.out.println("currentArea :"+currentArea);
	// Collection<String> areaTree = null;
	// if(null!=currentArea){
	// areaTree = new ArrayList<String>();
	// Area area = areaService.find(Long.valueOf(currentArea));
	// Set<Area> areaChild = area.getChildren();
	// Iterator<Area> iterator = areaChild.iterator();
	// areaTree.add(currentArea.toString());
	// while(iterator.hasNext()){
	// String sonId = iterator.next().getId().toString();
	// System.out.println("Test :"+sonId);
	// areaTree.add(sonId);
	// Area loopArea = null;
	// do{
	// if(null!=loopArea){
	// Iterator<Area> childArea = loopArea.getChildren().iterator();
	// while(childArea.hasNext()){
	// String xxtemp = childArea.next().getId().toString();
	// System.out.println("xxtemp :"+xxtemp);
	// areaTree.add(xxtemp);
	// loopArea = null;
	// }
	// }else{
	// loopArea = areaService.find(Long.valueOf(sonId));
	// }
	// }while(null!=loopArea && null != loopArea.getChildren() &&
	// loopArea.getChildren().size() > 0);
	// }
	// }
	//
	//
	//
	// Page<CriminalBase> page = criminalBaseService.findPage(pageable,
	// idCardNum, name, criminalCategory, penaltyCategory, temporaryGuy,
	// areaTree, fromDate, toDate);
	//
	// List<CriminalBase> contentAfterFilter = new ArrayList<CriminalBase>();
	// for(CriminalBase criminalBase : page.getContent()){
	// String enthnicName =
	// ethnicService.find(Long.valueOf(criminalBase.getEthnic())).getName();
	// criminalBase.setEthnic(enthnicName);
	// if(null != criminalCategory){
	// //filter out the objects which do NOT match the where clause
	// String []rest = criminalBase.getCaseCriminal().split(",");
	// for(String str : rest){
	// if(str.equals(criminalCategory)){
	// contentAfterFilter.add(criminalBase);
	// break;
	// }else{
	// continue;
	// }
	// }
	//
	// }
	// if(null != penaltyCategory){
	// //filter out the objects which do NOT match the where clause
	// String []rest = criminalBase.getCasePenalty().split(",");
	// for(String str : rest){
	// if(str.equals(penaltyCategory)){
	// if(contentAfterFilter.indexOf(criminalBase) == -1){
	// contentAfterFilter.add(criminalBase);
	// }
	// break;
	// }else{
	// continue;
	// }
	// }
	//
	// }
	// }
	//
	// if(contentAfterFilter.size() != 0){
	// page.setContent(contentAfterFilter);
	// }
	// model.addAttribute("commonBool",
	// masterConfigService.findList(KeyType.COMMON_BOOLEAN.getValue()));
	// model.addAttribute("genderGroup",
	// masterConfigService.findList(KeyType.GENDER.getValue()));
	// model.addAttribute("page", page);
	// //where clause for displaying
	// model.addAttribute("displayIdCardNum",idCardNum);
	// model.addAttribute("displayName",name);
	// model.addAttribute("displayCriminalCategory",criminalCategory);
	// model.addAttribute("displayPenaltyCategory",penaltyCategory);
	// model.addAttribute("displayTemporaryGuy",temporaryGuy);
	// model.addAttribute("displayCurrentArea", currentArea);
	// if(null != fromDate && null != toDate){
	// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	// model.addAttribute("diaplayFromDate", sdf.format(fromDate));
	// model.addAttribute("diaplayToDate", sdf.format(toDate));
	// }
	// if(null != currentArea){
	// model.addAttribute("currentArea", areaService.find(new
	// Long(currentArea)));
	// }
	//
	// model.addAttribute("professionGroup",
	// masterConfigService.findList(KeyType.PROFESSION.getValue()));
	// model.addAttribute("penaltyCategoryGroup",
	// masterConfigService.findList(KeyType.PENALTY_CATEGORY.getValue()));
	// model.addAttribute("criminalCategoryGroup",
	// masterConfigService.findList(KeyType.CRIMINAL_CATEGORY.getValue()));
	// return "/admin/criminalbase/list";
	// }

	// /**
	// * 删除
	// */
	// @RequestMapping(value = "/delete", method = RequestMethod.POST)
	// public @ResponseBody
	// Message delete(Long[] ids) {
	// if (ids != null) {
	// for (Long id : ids) {
	// CriminalBase criminalBase = criminalBaseService.find(id);
	// if (null != criminalBase) {
	// criminalBase.setStatus("1");
	// criminalBaseService.update(criminalBase);
	// }
	// else{
	// return Message.error("删除失败！", criminalBase.getName());
	// }
	// }
	// }
	// return SUCCESS_MESSAGE;
	// }

	/**
	 * 查看
	 */
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String view(String idCardNum, String id, ModelMap model) {
		CriminalBase criminalBase = criminalBaseService.find(new Long(id));
		model.addAttribute("commonHave", masterConfigService.findList(KeyType.COMMON_HAVE.getValue()));
		model.addAttribute("commonBool", masterConfigService.findList(KeyType.COMMON_BOOLEAN.getValue()));
		// model.addAttribute("ethnic", ethnicService.find(new
		// Long(criminalBase.getEthnic())));
		// model.addAttribute("religion", religionService.find(new
		// Long(criminalBase.getReligion())));
		model.addAttribute("areaGroup", areaService.findAll());
		model.addAttribute("genderGroup", masterConfigService.findList(KeyType.GENDER.getValue()));
		model.addAttribute("marriageGroup", masterConfigService.findList(KeyType.MARRIAGE_STATUS.getValue()));
		model.addAttribute("durationStayGroup", masterConfigService.findList(KeyType.DURATION_STATUS.getValue()));
		model.addAttribute("educationGroup", masterConfigService.findList(KeyType.EDUCATION_LEVEL.getValue()));
		model.addAttribute("mainIncomeGroup", masterConfigService.findList(KeyType.TEMPORARY_INCOME.getValue()));
		model.addAttribute("professionGroup", masterConfigService.findList(KeyType.PROFESSION.getValue()));
		model.addAttribute("penaltyCategoryGroup", masterConfigService.findList(KeyType.PENALTY_CATEGORY.getValue()));
		model.addAttribute("criminalCategoryGroup", masterConfigService.findList(KeyType.CRIMINAL_CATEGORY.getValue()));
		model.addAttribute("criminalCategoryGroup", masterConfigService.findList(KeyType.CRIMINAL_CATEGORY.getValue()));
		model.addAttribute("penaltyCategoryGroup", masterConfigService.findList(KeyType.PENALTY_CATEGORY.getValue()));
		model.addAttribute("temporaryNoRegistDuraGroup",
				masterConfigService.findList(KeyType.TEMPORARY_NO_REGIST_DURA.getValue()));
		model.addAttribute("temporaryTransToolsGroup",
				masterConfigService.findList(KeyType.TEMPORARY_TRANS_TOOL.getValue()));
		model.addAttribute("localAreaCategoryGroup",
				masterConfigService.findList(KeyType.CIVIL_DIS_FOOTHOLD_PLACE.getValue()));
		model.addAttribute("criminalBase", criminalBase);
		return "/admin/criminalbase/view";
	}

	/**
	 * 编辑
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(String idCardNum, String id, ModelMap model) {
		CriminalBase criminalBase = criminalBaseService.find(new Long(id));
		model.addAttribute("commonHave", masterConfigService.findList(KeyType.COMMON_HAVE.getValue()));
		model.addAttribute("commonBool", masterConfigService.findList(KeyType.COMMON_BOOLEAN.getValue()));

		model.addAttribute("ethnicGroup", ethnicService.findAll());
		model.addAttribute("religionGroup", religionService.findAll());
		// model.addAttribute("ethnic", ethnicService.find(new
		// Long(criminalBase.getEthnic())));
		// model.addAttribute("religion", religionService.find(new
		// Long(criminalBase.getReligion())));
		// model.addAttribute("currentAreaTemp", areaService.find(new
		// Long(criminalBase.getCurrentArea())));
		// model.addAttribute("householdAreaTemp", areaService.find(new
		// Long(criminalBase.getHouseholdArea())));
		model.addAttribute("localCriminalAreaTemp", areaService.find(new Long(criminalBase.getLocalCriminalArea())));

		model.addAttribute("genderGroup", masterConfigService.findList(KeyType.GENDER.getValue()));
		model.addAttribute("marriageGroup", masterConfigService.findList(KeyType.MARRIAGE_STATUS.getValue()));
		model.addAttribute("durationStayGroup", masterConfigService.findList(KeyType.DURATION_STATUS.getValue()));
		model.addAttribute("educationGroup", masterConfigService.findList(KeyType.EDUCATION_LEVEL.getValue()));
		model.addAttribute("mainIncomeGroup", masterConfigService.findList(KeyType.TEMPORARY_INCOME.getValue()));
		model.addAttribute("professionGroup", masterConfigService.findList(KeyType.PROFESSION.getValue()));
		model.addAttribute("penaltyCategoryGroup", masterConfigService.findList(KeyType.PENALTY_CATEGORY.getValue()));
		model.addAttribute("criminalCategoryGroup", masterConfigService.findList(KeyType.CRIMINAL_CATEGORY.getValue()));
		model.addAttribute("criminalCategoryGroup", masterConfigService.findList(KeyType.CRIMINAL_CATEGORY.getValue()));
		model.addAttribute("penaltyCategoryGroup", masterConfigService.findList(KeyType.PENALTY_CATEGORY.getValue()));
		model.addAttribute("temporaryNoRegistDuraGroup",
				masterConfigService.findList(KeyType.TEMPORARY_NO_REGIST_DURA.getValue()));
		model.addAttribute("temporaryTransToolsGroup",
				masterConfigService.findList(KeyType.TEMPORARY_TRANS_TOOL.getValue()));
		model.addAttribute("localAreaCategoryGroup",
				masterConfigService.findList(KeyType.CIVIL_DIS_FOOTHOLD_PLACE.getValue()));
		model.addAttribute("criminalBase", criminalBase);
		return "/admin/criminalbase/edit";
	}

	/**
	 * 更新
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(CriminalBase criminalBase, RedirectAttributes redirectAttributes) {

		// criminalBase.setStatus("0");
		// Admin admin = adminService.getCurrent();
		// criminalBase.setPolicerDepartment(admin.getDepartment());
		// criminalBase.setPolicerName(admin.getName());
		criminalBaseService.update(criminalBase);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);

		// if(null!=caseCriminal && !"".equals(caseCriminal)){
		// return
		// "redirect:/admin/robsteal/edit.jhtml?criminalNumber="+criminalBase.getCriminalNumber();
		// }
		//
		// if(null!=casePenalty && !"".equals(casePenalty)){
		// return
		// "redirect:/admin/civildispute/edit.jhtml?criminalNumber="+criminalBase.getCriminalNumber();
		// }
		return "redirect:list.jhtml";
	}

	@RequestMapping(value = "/pageRedirect", method = RequestMethod.GET)
	public String pageRedirect(String criminalNumber, String caseCriminal, String casePenalty, ModelMap model) {
		if (null != casePenalty && !"".equals(casePenalty)) {
			return "redirect:/admin/civildispute/view.jhtml?criminalNumber=" + criminalNumber;
		}
		if (null != caseCriminal && !"".equals(caseCriminal)) {
			return "redirect:/admin/robsteal/view.jhtml?criminalNumber=" + criminalNumber;
		}
		return "";
	}

	private String exchange(Object obj) {
		return (String) obj;
	}

}
